Perl Web Development: From Novice To Professional by B. Brown Jason
Author:B. Brown, Jason [B. Brown, Jason]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2020-10-19T16:00:00+00:00
Compare this output with that shown in Figure 11-12. This output was produced by placing the output from the args() method into a hash, as shown in Listing 11-6. Notice that only the final value for the action parameter is actually sent to output.
Figure 11-12. Output from a multivalued query string
Although you can iterate through multivalued parameters, if your CGI program will be using multivalued parameters, I recommend using the CGI.pmâs methods for accessing these values, rather than coding around the args() method.
So far, these methods retrieve values from GET requests. For POST requests, use the content() method, as shown in Listing 11-7 (Post.cgi).
Listing 11-7. Using the content() Method
use strict;
my $r = shift;
$r->send_http_header('text/plain');
my %data = $r->content;
foreach my $name (keys %data) {
print "$name = $data{$name}
";
}
This code looks and is largely similar to Listing 11-6, which retrieved arguments using the args() method. Mainly, the variable names have been changed to protect the innocent. The content() in this case.
The Apache::Request class provides the most flexible method for working with form data through the param() method, as in the example in Listing 11-8 (Param.cgi).
Listing 11-8. Using the param() Method to Look at Parameters use Apache::Request;
my $r = Apache::Request->new(shift);
$r->send_http_header('text/plain');
foreach my $param ($r->param) {
print "Param: $param = " , $r->param($param), "
";
}
The param() method works like CGI.pm, but is implemented in the C programming language as opposed to Perl. The param() method works with both GET and POST data.
â Note The examples in this section also demonstrate how to work with HTML form fields through
mod_perl. They show how to parse the parameters from the GET or POST request.
Other methods related to the incoming request include header_only(), for determining whether or not the incoming request was aHEAD type request, and proxyreq(), for determining whether or not the request is a proxy request. Both of these methods return true if they are positive, meaning that the method will return true if itâs aHEAD request or if itâs a proxy request, respectively.
Accessing Request Headers
So far, youâve seen the actual request and methods for working with the request. As noted earlier, the client may also send headers related to the request.
You can access request headers through the headers_in() method, which contains a set of name =value pairs. The headers_in() method is part of the Apache::Table class, which also contains other class methods, including the following:
⢠err_headers_out()
⢠headers_out()
⢠info()
⢠notes()
⢠subprocess_env()
Each of the class methods of Apache::Table has its own set of methods, including the following:
⢠add()
⢠clear()
⢠do()
⢠get()
⢠merge()
⢠new()
⢠set()
⢠unset()
Since name =value pairs are sent from the headers_in() method, the output naturally lends itself to being represented in a hash. Listing 11-9 (Headersin.cgi) prints the output from the headers_in() method.
Listing 11-9. The headers_in() Method to See Name=Value Pairs use strict;
my $r = shift;
$r->send_http_header('text/plain');
my %headers = $r->headers_in();
foreach my $header (keys %headers) { print "$header = $headers{$header}
";
}
The output is shown in Figure 11-13.
Figure 11-13. The headers for a request retrieved using the headers_in() method
You could access individual headers by calling them within the hash. For example, Listing 11-10 (Printuseragent.cgi) shows the code to see the user agent used for the request.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9926)
The Mikado Method by Ola Ellnestam Daniel Brolund(9786)
Dependency Injection in .NET by Mark Seemann(9348)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7789)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7566)
Svelte with Test-Driven Development by Daniel Irvine(7245)
Test-Driven Development with PHP 8 by Rainier Sarabia(6980)
Layered Design for Ruby on Rails Applications by Dementyev Vladimir;(6840)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6541)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6423)
Web Development with Django by Ben Shaw Saurabh Badhwar(6304)
React Application Architecture for Production by Alan Alickovic(6025)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5813)
Kotlin in Action by Dmitry Jemerov(5073)
Audition by Ryu Murakami(4592)
Software Architecture for Web Developers by Mihaela Roxana Ghidersa(4494)
Accelerating Server-Side Development with Fastify by Manuel Spigolon Maksim Sinik & Matteo Collina(4345)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4323)
Solidity Programming Essentials by Ritesh Modi(4050)
